﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunityServer.Groups;
using CommunityServer.Membership;
using NENAData.Factory;
using NENAData.LINQ;
//using CommunityServer.WebServices.Membership;

namespace NENAData.NENAMember
{
    public class NENAMember
    {

        private static MembershipDataContext GetSQLTransaction()
        {
            var x = new SQLMembership();
            return x.GetMembershipDB();
        }

        /// <summary>
        /// Return of 0 means true; a non-zero value is the Id of the user who currently owns this username
        /// </summary>
        /// <param name="intMemberId"></param>
        /// <returns></returns>
        //public void CreateCommunityServerUser(int intNenaMemberId)
        //{
        //MembershipDataContext db = GetSQLTransaction();
        //var oMember = (from m in db.Members
        //               where m.Member_ID == intMemberId
        //               select m).Single();


        //do not create the user if an ID is already set in their membership profile
        //if (oMember.CS_UserID > 1) return;
        //User oUser = new User
        //                 {
        //                     Username = oMember.LastName.Replace(" ", "") + oMember.FirstName.Replace(" ", ""),
        //                     Location = oMember.City + ", " + oMember.Province + ", " + oMember.Country,
        //                     PublicEmail = oMember.EmailAddress,
        //                     PrivateEmail = oMember.EmailAddress,
        //                     DisplayName = oMember.FirstName + " " + oMember.LastName,
        //                     Password = Account.Password.Generate(4),
        //                     JoinDate = oMember.Application_Date
        //                 };
        //APIUser oApiUser = new APIUser(); //old method

        //oApiUser.AddUser(oUser);
        // oAPIUser.AddUsertoRole(oUser.Username, Roles.Current_Member);
        //get the updated information from CS
        //User uExists = oApiUser.GetUser(oUser.Username);
        //oMember.CS_UserName = uExists.Username;
        //oMember.CS_UserID = uExists.Id;
        //oMember.Last_Modified = DateTime.Now;
        //db.SubmitChanges();
        //todo: enable to execute RecreatedAccount 
        //Email.Message.RecreatedAccount(oMember, oUser.Password);
        //Email.Message.CreatedNewAccount(oMember, oUser.Password);
        //}
        public void CreateTelligentCommunityUser(int intNenaMemberId)
        {

            NENAData.NENAMember.Ef ef = new Ef();
            NENAData.EF.Member efMember = ef.GetMember(intNenaMemberId);
            //do not create the user if an ID is already set in their membership profile
            if (efMember.CS_UserID > 1) return;

            //else create the user
            NENAData.NENAMember.TcMembership tcMember = new TcMembership();
            string Password = Account.Password.Generate(6);
            string TcUserName = efMember.LastName + efMember.FirstName;
            efMember.CS_UserName = TcUserName.Replace(" ", "");
            tcMember.AddUser(efMember, Password, efMember.CS_UserName);

            //update the membership db
            NENAData.NENAMember.TcXml tcXml = new TcXml();
            efMember.CS_UserID = tcXml.GetUserId(efMember.EmailAddress);
            efMember.Last_Modified = DateTime.Now;

            ef.UpdateMemberOnApplication(efMember);

            //oApiUser.AddUser(oUser);
            ////oAPIUser.AddUsertoRole(oUser.Username, Roles.Current_Member);
            ////get the updated information from CS
            //User uExists = oApiUser.GetUser(oUser.Username);
            //oMember.CS_UserName = uExists.Username;
            //oMember.CS_UserID = uExists.Id;
            //oMember.Last_Modified = DateTime.Now;
            //db.SubmitChanges();
            //Email.Message.CreatedNewAccount(oMember, oUser.Password);
        }

        public void UpdateMemberApplication(int intMemberID, MemberStatus.Status mStatusID)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.Member_ID == intMemberID
                           select m).Single();

            oMember.Account_Status = Convert.ToString((Int32)mStatusID);

            switch (mStatusID)
            {
                case MemberStatus.Status.Pending:
                    oMember.Application_Date = DateTime.Now;
                    break;
                case MemberStatus.Status.Expired:
                    oMember.Paid = "N";
                    break;
                case MemberStatus.Status.Paid:
                    oMember.Paid = "Y";
                    oMember.Date_Paid = DateTime.Now;
                    oMember.Date_Expired = DateTime.Now.AddYears(1);
                    break;
            }
            oMember.Last_Modified = DateTime.Now;
            db.SubmitChanges();
        }
        public void SetMemberReferralCode(int intMemberID, string strReferralCode)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.Member_ID == intMemberID
                           select m).Single();

            oMember.Referral_Code = strReferralCode;
            oMember.Last_Modified = DateTime.Now;
            db.SubmitChanges();
        }
        //public void UpdateMember_Paid(int intMemberID)
        //{
        //    MembershipDataContext db = GetSQLTransaction();

        //    var oMember = (from m in db.Members
        //                   where m.Member_ID == intMemberID
        //                   select m).Single();

        //    oMember.Account_Status = Convert.ToString((Int32)MemberStatus.Status.Paid);
        //    oMember.Last_Modified = DateTime.Now;
        //    oMember.Date_Paid = DateTime.Now;
        //    oMember.Paid = "Y";

        //    db.SubmitChanges();
        //}
        public bool UniqueCsUserName(string strUserName)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.CS_UserName == strUserName
                           select m);

            if (oMember.Count() > 0) return false;
            return true;
        }
        public bool UniqueCsUserName(Int32 intUserId)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.Member_ID == intUserId
                           select m).FirstOrDefault();

            string strAccountName = oMember.LastName + oMember.FirstName;
            strAccountName = strAccountName.Replace(" ", "");

            var oMemberCheck = (from m in db.Members
                                where m.CS_UserName == strAccountName
                                select m);

            if (oMemberCheck.Count() > 0) return false;
            return true;
        }
        public NENAData.LINQ.Member GetMember(int intMemberID)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.Member_ID == intMemberID
                           select m);

            return oMember.FirstOrDefault();
        }
        public NENAData.LINQ.Member GetMember(string strMemberEmail)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.EmailAddress == strMemberEmail
                           select m);

            return oMember.FirstOrDefault();
        }
        public NENAData.LINQ.Member GetMember_byCsUserName(string strCsUserName)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oMember = (from m in db.Members
                           where m.CS_UserName == strCsUserName
                           select m);

            return oMember.FirstOrDefault();
        }
        public List<NENAData.LINQ.Member> GetMembersForRebuild()
        {
            MembershipDataContext db = GetSQLTransaction();
            var oMember = (from m in db.Members
                           where m.Paid.Equals('Y') && m.CS_UserID.Equals(0)
                           select m).ToList();
            return oMember;
        }
        public string GetEmailAddresses(MemberStatus.Status oStatus)
        {
            MembershipDataContext db = GetSQLTransaction();
            int intStatus = (int)oStatus;

            var listMembers = (from m in db.Members
                               where m.Account_Status == intStatus.ToString()
                               select m).ToList();

            StringBuilder strEmailAddresses = new StringBuilder();
            foreach (var omember in listMembers)
            {
                strEmailAddresses.Append(omember.EmailAddress).Append(";");
            }
            return strEmailAddresses.ToString();

        }
        public string GetEmailAddresses(MemberStatus.Status oStatus, int intAffiliateID)
        {

            MembershipDataContext db = GetSQLTransaction();
            int intStatus = (int)oStatus;

            var listMembers = (from m in db.Members
                               where m.Account_Status == intStatus.ToString() &&
                               m.Affiliate_ID == intAffiliateID
                               select m).ToList();

            StringBuilder strEmailAddresses = new StringBuilder();

            foreach (var omember in listMembers)
            {
                strEmailAddresses.Append(omember.EmailAddress).Append(";");
            }

            return strEmailAddresses.ToString();
        }
        public List<NENAData.LINQ.Member> GetAllMembersAndApplicants()
        {
            MembershipDataContext db = GetSQLTransaction();
            var oMember = (from m in db.Members
                           select m).ToList();
            return oMember;
        }

    }
}